package com.cj.webapp.account.controller;

import com.cj.webapp.account.pojo.ConstantParamter;
import com.cj.webapp.common.sign.SignVerifyHandle;
import com.cj.webapp.common.utils.HFJsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 返回通知返回结果
 * Created by dell on 2017/12/25.
 */

public class AccountNoticeServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(AccountNoticeServlet.class);
    private static SignVerifyHandle signVerifyHandle=new SignVerifyHandle();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }
        // 请求参数
        //String requestJson = URLDecoder.decode(sb.toString(), "UTF-8");
        String requestJson =sb.toString();
        logger.info("异步通知返回报文信息："+requestJson);
        try {Map<String, String> jsonToMap = HFJsonUtil.getJsonToMap(requestJson);
            Map<String, String> headMap = HFJsonUtil.getJsonToMap(jsonToMap.get("head"));
            Map<String,String> mapRes=new HashMap<>();
            if("000000".equals(headMap.get("code"))){
                if(signVerifyHandle.verifySign(ConstantParamter.VIP_NUM,jsonToMap.get("body"), headMap.get("signData"))){
                    mapRes= HFJsonUtil.getJsonToMap(jsonToMap.get("body"));
                    //如果成功跳转到开户成功页面
                    request.setAttribute("mapRes",mapRes);
                    //request.getRequestDispatcher("/page/mobile/notice/successNotice.jsp").forward(request,response);
                    //response.sendRedirect("/page/mobile/notice/successNotice.jsp");
                    logger.info("跳转到成功页面，异步通知成功");


                }else{
                    logger.info("用户信息查询返回信息:验签失败");
                }
            }else if("100008".equals(headMap.get("code"))){
                mapRes.put("error","用户信息不存在");
            }else if("100001".equals(headMap.get("code"))){
                mapRes.put("error","签名验证失败");
            }else if("100002".equals(headMap.get("code"))){
                mapRes.put("error","TOKEN验证失败");
            }else if("100003".equals(headMap.get("code"))){
                mapRes.put("error","TOKEN失效");
            }else if("100004".equals(headMap.get("code"))){
                mapRes.put("error","平台编号不存在");
            }else if("100005".equals(headMap.get("code"))){
                mapRes.put("error","平台状态异常");
            }else if("100006".equals(headMap.get("code"))){
                mapRes.put("error","请求流水号重复");
            }else if("100007".equals(headMap.get("code"))){
                mapRes.put("error","参数必输项校验未通过");
            }else if("100010".equals(headMap.get("code"))){
                mapRes.put("error","该用户已存在,请勿重复注册!");
            }else if("100011".equals(headMap.get("code"))){
                mapRes.put("error","暂无产品信息");
            }

        }catch (Exception e){
            e.printStackTrace();
            logger.info("对不起，报错啦！！接收的数据"+requestJson);
        }

    }
}
